package com.yqy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqy.entity.StudentGrade;
import com.yqy.entity.StudentInfo;
import com.yqy.entity.User;
import com.yqy.mapper.StudentGradeMapper;
import com.yqy.mapper.StudentInfoMapper;
import com.yqy.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author yiqianyi
 * @date 2022/3/16 20:07
 */
@RestController
@CrossOrigin
@Api(tags = {"登录注册控制层"})
public class LoginController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StudentInfoMapper studentInfoMapper;
    @Autowired
    private StudentGradeMapper studentGradeMapper;

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "login")
    public List login(@RequestBody @ApiParam(name = "user对象", value = "user", required = true) User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("account", user.getAccount());
        queryWrapper.eq("password", user.getPassword());
        return userMapper.selectList(queryWrapper);
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册", notes = "register")
    public int register(@RequestBody @ApiParam(name = "user对象", value = "user", required = true) User user) {
        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setAccount(user.getAccount());
        studentInfo.setUsername(user.getUsername());
        studentInfo.setAuth(user.getAuth());
        studentInfoMapper.insert(studentInfo);

        StudentGrade studentGrade = new StudentGrade();
        studentGrade.setUsername(user.getUsername());
        studentGradeMapper.insert(studentGrade);

        return userMapper.insert(user);
    }
}
